Passed
Push — master ( 222441...bcf585 )
by Jean
03:00
created

$.fn.enableNoUISlider   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
nc 2
dl 0
loc 40
c 1
b 0
f 0
rs 7.8799
nop 0
1
const $ = require('jquery'); // eslint-disable-line import/no-unresolved
2
const NoUISlider = require('nouislider');
3
const wNumb = require('wnumb');
4
5
$.fn.enableNoUISlider = function() {
6
    this.filter('input[type=range]:not(.js-nouislider-initialized)').each(function() {
7
        const $rangeInput = $(this);
8
9
        $rangeInput.css({display: 'none'}).addClass('js-nouislider-initialized');
10
11
        const $wrapper = $('<div class="nouislider-range-wrapper"><div></div></div>')
12
            .insertAfter($rangeInput);
13
14
        const min         = $rangeInput.attr('min')          ? Number($rangeInput.attr('min'))   : 0;
15
        const max         = $rangeInput.attr('max')          ? Number($rangeInput.attr('max'))   : 100;
16
        const step        = $rangeInput.attr('step')         ? Number($rangeInput.attr('step'))  : 1;
17
        const value       = $rangeInput.attr('value')        ? Number($rangeInput.attr('value')) : 1;
18
        const wnumbFormat = $rangeInput.data('wnumb-format') ? $rangeInput.data('wnumb-format')  : null;
19
20
        const options = {
21
            start: value, // [value, value2...]
22
            step,
23
            // connect: false,  // Display colored bars between handles
24
            range: {
25
                min,
26
                max,
27
            },
28
            tooltips: [true],
29
        };
30
31
        if (wnumbFormat !== null) {
32
            options.format = wNumb(wnumbFormat);
33
        }
34
35
        const noUiSliderInstance = NoUISlider.create($wrapper.find('div').get(0), options);
36
37
        $rangeInput.on('change', function () {
38
            noUiSliderInstance.set(this.value);
39
        });
40
41
        noUiSliderInstance.on('change', (values, handle, unencoded, tap, positions) => {
0 ignored issues
show
Unused Code introduced by
The parameter tap is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter positions is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
42
            $rangeInput.val(unencoded[0]).trigger('change');
43
            noUiSliderInstance.set(unencoded[0]);
44
        });
45
    });
46
};
47